/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.utilities.Rater;

import java.awt.Dimension;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

import javax.swing.JOptionPane;

import core.exceptions.DBAccessException;
import core.objects.Book;
import core.objects.Country;
import core.objects.User;
import core.objects.UserComment;
import db.DBAccessEngine;
import db.MySQLFormatDate;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

/**
 *
 * @author abadi
 */
public class JPanelUserInfo extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    public static final int OPEN_PANEL_HEIGHT = 400;
    public static final int CLOSE_PANEL_HEIGHT = 90;
    private static final String USER_COMMENT_INITIAL_VALUE = "Did you enjoy trading books with me? Please recommand about me.";
    private Rater myUserRater;
    private Rater usersRater;
    private User user;

    /**
     * Creates new form BookPanel
     */
    public JPanelUserInfo(User user) {
        this.user = user;
        initComponents();
        initHeaderComponents();
        setMaximumHeight(CLOSE_PANEL_HEIGHT);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelUserInfoHeaderWithPic = new javax.swing.JPanel();
        jPanelUserHeader = new javax.swing.JPanel();
        jLabelFirstName = new javax.swing.JLabel();
        jLabelLastName = new javax.swing.JLabel();
        jLabelGender = new javax.swing.JLabel();
        jLabelBirthday = new javax.swing.JLabel();
        jLabelCountry = new javax.swing.JLabel();
        jPanelUserTitle = new javax.swing.JPanel();
        jLabelUserName = new javax.swing.JLabel();
        jLabelEmail = new javax.swing.JLabel();
        jLabelEnlarge = new javax.swing.JLabel();
        jPanelRating = new javax.swing.JPanel();
        jLabelImage = new javax.swing.JLabel();
        jTabbedPane = new javax.swing.JTabbedPane();
        jPanelUserBooks = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jPanelUserComments = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaUserComment = new javax.swing.JTextArea();
        jPanelUserRater = new javax.swing.JPanel();
        jLabelSubmitButton = new javax.swing.JLabel();
        jScrollPaneUsersComments = new javax.swing.JScrollPane();
        jPanelComments = new javax.swing.JPanel();

        setAlignmentX(0.0F);
        setAlignmentY(0.0F);
        setOpaque(false);

        jPanelUserInfoHeaderWithPic.setOpaque(false);

        jPanelUserHeader.setOpaque(false);

        jLabelFirstName.setText("First Name:");

        jLabelLastName.setText("Last Name:");

        jLabelGender.setText("Gender: ");

        jLabelBirthday.setText("Birthday: ");

        jLabelCountry.setText("Country:");

        jPanelUserTitle.setOpaque(false);

        jLabelUserName.setText("User Name");

        jLabelEmail.setText("[Email]");
        jLabelEmail.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelEmailMouseClicked(evt);
            }
        });
        jLabelEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
			public void keyPressed(java.awt.event.KeyEvent evt) {
                jLabelEmailKeyPressed(evt);
            }
        });

        jLabelEnlarge.setText("[+]");
        jLabelEnlarge.setToolTipText("Sounds Interesting...");
        jLabelEnlarge.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelEnlargeMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelUserTitleLayout = new javax.swing.GroupLayout(jPanelUserTitle);
        jPanelUserTitle.setLayout(jPanelUserTitleLayout);
        jPanelUserTitleLayout.setHorizontalGroup(
            jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserTitleLayout.createSequentialGroup()
                .addComponent(jLabelEnlarge)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelEmail)
                .addGap(99, 99, 99))
        );
        jPanelUserTitleLayout.setVerticalGroup(
            jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabelUserName)
                .addComponent(jLabelEmail)
                .addComponent(jLabelEnlarge))
        );

        jPanelRating.setOpaque(false);
        jPanelRating.setLayout(new java.awt.GridLayout(1, 1));

        javax.swing.GroupLayout jPanelUserHeaderLayout = new javax.swing.GroupLayout(jPanelUserHeader);
        jPanelUserHeader.setLayout(jPanelUserHeaderLayout);
        jPanelUserHeaderLayout.setHorizontalGroup(
            jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                        .addComponent(jPanelUserTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(58, 58, 58)
                        .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                        .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelLastName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelBirthday, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelCountry, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelGender, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanelUserHeaderLayout.setVerticalGroup(
            jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelUserHeaderLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelUserTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFirstName)
                    .addComponent(jLabelGender)
                    .addComponent(jLabelBirthday))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUserHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelLastName)
                    .addComponent(jLabelCountry))
                .addContainerGap())
        );

        jLabelImage.setAlignmentY(0.0F);
        jLabelImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 51, 51), 1, true));
        jLabelImage.setMaximumSize(new java.awt.Dimension(20, 2));
        jLabelImage.setPreferredSize(new java.awt.Dimension(2, 90));

        javax.swing.GroupLayout jPanelUserInfoHeaderWithPicLayout = new javax.swing.GroupLayout(jPanelUserInfoHeaderWithPic);
        jPanelUserInfoHeaderWithPic.setLayout(jPanelUserInfoHeaderWithPicLayout);
        jPanelUserInfoHeaderWithPicLayout.setHorizontalGroup(
            jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelUserHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelUserInfoHeaderWithPicLayout.setVerticalGroup(
            jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanelUserHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jTabbedPane.setBackground(new java.awt.Color(51, 153, 255));

        jPanelUserBooks.setOpaque(false);
        jPanelUserBooks.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setOpaque(false);

        jPanel1.setBackground(new java.awt.Color(51, 153, 255));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane2.setViewportView(jPanel1);

        jPanelUserBooks.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jTabbedPane.addTab("User Books", jPanelUserBooks);

        jPanelUserComments.setBackground(new java.awt.Color(51, 153, 255));
        jPanelUserComments.setAlignmentX(0.0F);
        jPanelUserComments.setAlignmentY(0.0F);
        jPanelUserComments.setOpaque(false);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTextAreaUserComment.setBackground(new java.awt.Color(230, 244, 255));
        jTextAreaUserComment.setColumns(20);
        jTextAreaUserComment.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextAreaUserComment.setLineWrap(true);
        jTextAreaUserComment.setRows(5);
        jTextAreaUserComment.setText(USER_COMMENT_INITIAL_VALUE);
        jTextAreaUserComment.setWrapStyleWord(true);
        jTextAreaUserComment.setAlignmentX(0.0F);
        jTextAreaUserComment.setAlignmentY(0.0F);
        jTextAreaUserComment.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTextAreaUserComment.setPreferredSize(new java.awt.Dimension(204, 20));
        jTextAreaUserComment.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusGained(java.awt.event.FocusEvent evt) {
                jTextAreaUserCommentFocusGained(evt);
            }
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextAreaUserCommentFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(jTextAreaUserComment);

        jPanelUserRater.setOpaque(false);
        jPanelUserRater.setLayout(new java.awt.GridLayout(1, 0));

        jLabelSubmitButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabelSubmitButton.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabelSubmitButton.setAlignmentY(0.0F);
        jLabelSubmitButton.setFocusCycleRoot(true);
        jLabelSubmitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelSubmitButtonMouseClicked(evt);
            }
        });

        jScrollPaneUsersComments.setBackground(new java.awt.Color(51, 153, 255));
        jScrollPaneUsersComments.setBorder(null);
        jScrollPaneUsersComments.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPaneUsersComments.setAlignmentX(0.0F);
        jScrollPaneUsersComments.setAlignmentY(0.0F);

        jPanelComments.setBackground(new java.awt.Color(51, 153, 255));
        jPanelComments.setLayout(new javax.swing.BoxLayout(jPanelComments, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPaneUsersComments.setViewportView(jPanelComments);

        javax.swing.GroupLayout jPanelUserCommentsLayout = new javax.swing.GroupLayout(jPanelUserComments);
        jPanelUserComments.setLayout(jPanelUserCommentsLayout);
        jPanelUserCommentsLayout.setHorizontalGroup(
            jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneUsersComments)
                    .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanelUserRater, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelSubmitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelUserCommentsLayout.setVerticalGroup(
            jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                        .addComponent(jPanelUserRater, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelSubmitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneUsersComments, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
        );

        jTabbedPane.addTab("User Comments", jPanelUserComments);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelUserInfoHeaderWithPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelUserInfoHeaderWithPic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jLabelEnlargeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelEnlargeMouseClicked
        if (getMaximumSize().height == CLOSE_PANEL_HEIGHT) {
            setMaximumHeight(OPEN_PANEL_HEIGHT);
            jLabelEnlarge.setText("[-]");
            if (myUserRater == null) { // expanding panels aren't initialized yet
                initBooksListPanel();
                initUserCommentsPanel();
            }
        } else {
            setMaximumHeight(CLOSE_PANEL_HEIGHT);
            jLabelEnlarge.setText("[+]");
        }

        setVisible(false);
        setVisible(true);
    }//GEN-LAST:event_jLabelEnlargeMouseClicked

    private void jLabelSubmitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelSubmitButtonMouseClicked
        if (!jLabelSubmitButton.isEnabled()){
            return;
        }
        if (myUserRater.getRating() == 0) {
            JOptionPane.showMessageDialog(this, "Please vote for the user rating before submitting.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        String textComment = jTextAreaUserComment.getText();
        if (textComment.equals(USER_COMMENT_INITIAL_VALUE)) {
            textComment = "(no content)";
        }
        UserComment comment = GUIUtils.addUserComment(user, MainForm.getMyUser(), textComment, myUserRater.getRating(), this);
        disableCommentSubmission();

        jPanelComments.add(new JPanelComment(comment), 0);
        usersRater.setRating((int) Math.round(((double) (usersRater.getVoters() * usersRater.getRating() + comment.getRating())) / (usersRater.getVoters() + 1)));
        usersRater.setVoters(usersRater.getVoters() + 1);

    }//GEN-LAST:event_jLabelSubmitButtonMouseClicked

    private void jTextAreaUserCommentFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaUserCommentFocusGained
        GUIUtils.handleJTextFieldFocusGained(jTextAreaUserComment, USER_COMMENT_INITIAL_VALUE);
    }//GEN-LAST:event_jTextAreaUserCommentFocusGained

    private void jTextAreaUserCommentFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaUserCommentFocusLost
        GUIUtils.handleJTextFieldFocusLost(jTextAreaUserComment, USER_COMMENT_INITIAL_VALUE);
    }//GEN-LAST:event_jTextAreaUserCommentFocusLost

    private void jLabelEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jLabelEmailKeyPressed
    }//GEN-LAST:event_jLabelEmailKeyPressed

    private void jLabelEmailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelEmailMouseClicked
        try {
            GUIUtils.mailto(user.getEmail(), "Hello Loan-a-Book Friend!", "");
        } catch (IOException|URISyntaxException ex) {
            JOptionPane.showMessageDialog(this, "Loan-a-Book failed to open your email client.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jLabelEmailMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelBirthday;
    private javax.swing.JLabel jLabelCountry;
    private javax.swing.JLabel jLabelEmail;
    private javax.swing.JLabel jLabelEnlarge;
    private javax.swing.JLabel jLabelFirstName;
    private javax.swing.JLabel jLabelGender;
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JLabel jLabelLastName;
    private javax.swing.JLabel jLabelSubmitButton;
    private javax.swing.JLabel jLabelUserName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelComments;
    private javax.swing.JPanel jPanelRating;
    private javax.swing.JPanel jPanelUserBooks;
    private javax.swing.JPanel jPanelUserComments;
    private javax.swing.JPanel jPanelUserHeader;
    private javax.swing.JPanel jPanelUserInfoHeaderWithPic;
    private javax.swing.JPanel jPanelUserRater;
    private javax.swing.JPanel jPanelUserTitle;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPaneUsersComments;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JTextArea jTextAreaUserComment;
    // End of variables declaration//GEN-END:variables

    private void initHeaderComponents() {
        jLabelUserName.setText("<html><u>" + user.getName() + "</u></html>");
        //bookPanel(book);
        GUIUtils.setProfileImageAtLabel(user, jLabelImage);

        GUIUtils.setJComponentFontAndColor(jLabelUserName, 15);
        GUIUtils.initTextHeaderJlabel(jLabelFirstName, user.getFirstName());
        GUIUtils.initTextHeaderJlabel(jLabelGender, user.getGender().toString());
        GUIUtils.initTextHeaderJlabel(jLabelBirthday, MySQLFormatDate.toString(user.getBirthday()));
        Country country = null;
        if (user.getCountryID() != 0) {
            try {
                country = (Country) DBAccessEngine.Country().select(user.getCountryID()).get(0);
            } catch (DBAccessException ex) {
                GUIUtils.showMessageError(ex, this);
            }
        }
        GUIUtils.initTextHeaderJlabel(jLabelCountry, (country == null ? "" : country.getName()));
        GUIUtils.initTextHeaderJlabel(jLabelLastName, user.getLastName());

        GUIUtils.setURLLink(jLabelEmail, null, true);
        GUIUtils.setURLLink(jLabelEnlarge, null, false);

        int voters = 0;
        double rating = 0;
        try {
            voters = DBAccessEngine.UserComment().selectCount(user.getID(), null);
            rating = DBAccessEngine.UserComment().selectAvg(user.getID());
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
        usersRater = new Rater(((int) Math.round(rating)), voters);
        usersRater.setEnabled(false);
        jPanelRating.add(usersRater);

    }

    @SuppressWarnings("unchecked")
	private void initBooksListPanel() {
    	
    	final JPanelUserInfo thisObj = this;
    	DBAccessThread.invoke(new IDBAccessThread() {
			
			@Override
			public Object onCreateQuery() throws DBAccessException {
				return DBAccessEngine.User().selectAll(user.getID(), new Book(0, ""));			}
			
			@Override
			public void onAccessFinished(Object response) {
				List<Book> userBookList = (List<Book>) response;
		        for (Book book : userBookList) {
		            jPanel1.add(new JPanelBookInfoForUsersBookSearch(book,user));
		        }
			}
			
			@Override
			public void onAccessFailed(DBAccessException e) {
				GUIUtils.showMessageError(e, thisObj);
			}
		});
    	
    }

    private void initUserCommentsPanel() {
        GUIUtils.setImageAtSize(jLabelSubmitButton, 130, 30, "/resources/submit.png");
        GUIUtils.setURLLink(jLabelSubmitButton, null, false);
        myUserRater = new Rater(0, -1);
        jPanelUserRater.add(myUserRater);

        final JPanelUserInfo thisObj = this;
    	DBAccessThread.invoke(new IDBAccessThread() {
			
			@Override
			public Object onCreateQuery() throws DBAccessException {
				return DBAccessEngine.UserComment().select(user.getID());
			}
			
			@Override
			@SuppressWarnings("unchecked")
			public void onAccessFinished(Object response) {
				List<UserComment> comments = (List<UserComment>)response;
		        for (UserComment comment : comments) {
		            jPanelComments.add(new JPanelComment(comment));
		        }
		        
		        //we're already runningiin a separate thread:
	            try {
					if (DBAccessEngine.UserComment().exists(MainForm.getMyUser().getID(), user.getID(), new User(0, ""))) {
					    disableCommentSubmission();
					}
				} catch (DBAccessException exception) {
					//do nothing
				}
			}

			@Override
			public void onAccessFailed(DBAccessException e) {
				GUIUtils.showMessageError(e, thisObj);
			}
			
		});
        
    }


    private void setMaximumHeight(int height) {
        setMaximumSize(new Dimension(Integer.MAX_VALUE, height));
        setPreferredSize(new Dimension(getWidth(), height));
    }

    private void disableCommentSubmission() {
        jTextAreaUserComment.setText("Your comment is presented below.");
        jTextAreaUserComment.setEnabled(false);
        myUserRater.setEnabled(false);
        jLabelSubmitButton.setEnabled(false);
    }
}
